package generators;

import gamebackend.Block;

import java.util.Random;

public class ScaredGenerator extends AbstractRandomGenerator {
	public static final int SHY = 0;
	public static final int BRASH = 1;
	private final int my_mode;
	private final double my_chancePerBlock;

	public ScaredGenerator(Block the_block, Random the_random,
			double the_maxChance, double the_chancePerBlock, int the_mode) {
		super(the_block, the_random, the_maxChance);
		my_chancePerBlock = the_chancePerBlock;
		my_mode = the_mode;
	}

	@Override
	public void generate(Block[][] blocks) {
		final int row = blocks.length - 1;
		int[] distances = new int[blocks[row].length];
		boolean pathFound = false;
		int blocksFromPath = 0;
		for (int col = 0; col < distances.length; col++) {
			if (pathFound) {
				if (blocks[row][col].isPath()) {
					blocksFromPath = 0;
				} else {
					blocksFromPath++;
					distances[col] = blocksFromPath;
				}
				
			} else {
				if (blocks[row][col].isPath()) {
					pathFound = true;
				}
			}
		}
		pathFound = false;
		blocksFromPath = 0;
		for (int col = distances.length - 1; col >= 0; col--) {
			if (pathFound) {
				if (blocks[row][col].isPath()) {
					blocksFromPath = 0;
				} else {
					blocksFromPath++;
					if (distances[col] == 0 || distances[col] > blocksFromPath) {
						distances[col] = blocksFromPath;
					}
				}
				
			} else {
				if (blocks[row][col].isPath()) {
					pathFound = true;
				}
			}
		}
		
		for (int col = 0; col < distances.length; col++) {
			final double chance;
			if (blocks[row][col] != Block.EMPTY_BLOCK) {
				continue;
			}
			if (my_mode == SHY) {
				chance = distances[col] * my_chancePerBlock;
			} else {
				chance = getChance() - (distances[col]) * my_chancePerBlock;
			}
			
			if (distances[col] > 0 && getRandom().nextDouble() < chance) {
				blocks[row][col] = getBlock();
			}
		}
		
	}
	
	
	

}
